//
//  NetWorkTests.m
//  TribleWar
//
//  Created by cst_han on 12-8-9.
//  Copyright (c) 2012年 ＣＳＴ. All rights reserved.
//

#import "NetWorkHandlerTests.h"
#import "GameUser.pb.h"

//@implementation NetWorkHandlerTests
//
//
////Run before each test case
//-(void)setUp
//{
//    testNetWorkHandler_=[[NetWorkHandler alloc]init];
//}
//
////Run after each test case
//-(void)tearDown
//{
//    [testNetWorkHandler_ disConnect];
//    
//    testNetWorkHandler_=nil;
//}
//#pragma mark - test Initialize
///*
// init test issue:
// 1、not nil
// 2、variable has init
// */
//
//-(void)test_init
//{
//    //PreCondition: has init;
//    
//    GHAssertNotNil(testNetWorkHandler_, @"testNetWorkHandler should not be nil");
//    GHAssertNotNil([testNetWorkHandler_ sendBuf_], @"sendBuf_ should not be nil");
//    GHAssertNotNil([testNetWorkHandler_ recvBuf_], @"sendBuf_ should not be nil");
//    GHAssertNotNil([testNetWorkHandler_ userDefaults_], @"sendBuf_ should not be nil");
//    
//}
//
///*
// initConn test issue:
//    1、initialized handler?
//    2、NetWork Reachable?
//    3、isInConnecting ?
// */
//
//-(void)test_initConn_withHandlerUnInitialized
//{
//    testNetWorkHandler_ =nil;
//    
//    BOOL _rslt=[testNetWorkHandler_ initConn];
//    
//    GHAssertFalse(_rslt,@"with Uninitialized handler ,initConn should be failed");
//    
//  
//    GHAssertFalse( [testNetWorkHandler_ getIsInConnecting], @"with Uninitialized handler,isInConnecting should be NO");
//
//}
//
//-(void)test_initConn_netWorkReachable
//{
//    //Precondition: testing equipment's network should be on
//    BOOL _rslt=[testNetWorkHandler_ initConn];
//    
//    GHAssertTrue(_rslt, @"With Mobile NetWork Open,connecting...");
//    
//    [NSThread sleepForTimeInterval:5.0];
//    
//    _rslt=[testNetWorkHandler_ isSocketConnected];
//    
//    GHAssertTrue(_rslt,@"With Mobile NetWork Open,should be connected");
//}
//
//-(void)test_initConn_netWorkUnReachable
//{
//    
//    //Precondition: testing equipment's network should be off
//    
//    BOOL _rslt=[testNetWorkHandler_ initConn];
//    
//    GHAssertTrue(_rslt, @"With Mobile NetWork Open,connecting...");
//    
//    [NSThread sleepForTimeInterval:5.0];
//    
//    _rslt=[testNetWorkHandler_ isSocketConnected];
//    
//    GHAssertFalse(_rslt,@"With Mobile NetWork Open,should be connected");
//}
//
//#pragma mark - test Connection
//
///*
// connectToSrv test issue:
//    1、initialized handler?
//    2、isConnected?
//    3、isInConnecting ?before connect /after connect
//    4、Reconnect
// 
//    PS:test_initConn_netWorkReachable has cover up connectToSrv totoally correct one
//
// */
//-(void)test_connectToSrv_withHandlerUnInitialized
//{
//    //Precondition:testing equipment's network on
//    
//    BOOL _rslt=[testNetWorkHandler_ connectToSrv];
//    
//    GHAssertFalse(_rslt, @"with nil handler ,connectToSrv should be failed");
//    
//}
//
//-(void)test_connectToSrv_ifConnected
//{
//    //Precondition:testing equipment's network on
//    
//    BOOL _rslt=[testNetWorkHandler_ initConn];
//    
//    [NSThread sleepForTimeInterval:5.0];
//    
//    
//    _rslt=[testNetWorkHandler_ isSocketConnected];
//    
//    GHAssertTrue(_rslt, @"Should be connected");
//    
//    //after connection established
//    
//    _rslt=[testNetWorkHandler_ connectToSrv];
//    
//    GHAssertTrue(_rslt, @"ifConnected,connectToSrv should return with True");
//}
//
//-(void)test_connectToSrv_ifIsInConnecting
//{
//     //Precondition:testing equipment's network on
//    [testNetWorkHandler_ setIsInConnecting:YES];
//    
//    
//    BOOL _rslt=[testNetWorkHandler_ connectToSrv];
//    GHAssertFalse(_rslt, @"With isInConnecting On, connectToSrv should return with NO");
//    
//}
//
//-(void)test_connectToSrv_whileReConnect
//{
//    
//    BOOL _rslt=[testNetWorkHandler_ initConn];
//    
//    [NSThread sleepForTimeInterval:5.0];
//    
//    
//    _rslt=[testNetWorkHandler_ isSocketConnected];
//    
//    GHAssertTrue(_rslt, @"Should be connected");
//    
//    //after connection established,
//    //make it disconnect
//    [testNetWorkHandler_ disConnect];
//    
//    
//    //then reconnect
//    
//    _rslt=[testNetWorkHandler_ connectToSrv];
//    
//    GHAssertTrue(_rslt, @"reconnect should be success");
//    
//    
//    [NSThread sleepForTimeInterval:5.0];
//    
//    
//    _rslt=[testNetWorkHandler_ isSocketConnected];
//    
//    GHAssertTrue(_rslt, @"Should be connected");
//    
//}
//
///*
// disconnect test issue:
//    1、initiative disconnect
//    2、network problem cause handle 
//    3、initialized handler (including unconnected)
// */
//
//-(void)test_disconnect
//{
//    //Precondition:testing equipment's network on
//    
//    BOOL _rslt=[testNetWorkHandler_ initConn];
//    
//    [NSThread sleepForTimeInterval:5.0];
//    
//    
//    _rslt=[testNetWorkHandler_ isSocketConnected];
//    
//    GHAssertTrue(_rslt, @"Should be connected");
//    
//    _rslt=[testNetWorkHandler_ disConnect];
//    
//    GHAssertTrue(_rslt, @"Disconnect should be success");
//}
//
//-(void)test_disconnect_withHandlerUnInitialized
//{
//    BOOL _rslt=[testNetWorkHandler_ disConnect];
//    
//    GHAssertTrue(_rslt, @"even if handler is nli,disConnect should return YES");
//}
//
//-(void)test_disconnect_netWorkUnReachable
//{
//    
//    BOOL _rslt=[testNetWorkHandler_ disConnect];
//    GHAssertTrue(_rslt, @"even if network unreachable nli,disConnect should return YES");
//}
//
//#pragma mark - test isSocketConnected
//
///*
// isSocketConnected test issue:
// 1、initialized handler?
// 2、socket ==/!= nil.
// 3、disConnected/connected.
// */
//
//-(void)test_isSocketConnected_ifConnected
//{
//    //Precondition: testing equipment's network should be on;
//    BOOL _rslt=[testNetWorkHandler_ initConn];
//    
//    [NSThread sleepForTimeInterval:5.0];
//    
//    _rslt=[testNetWorkHandler_ isSocketConnected];
//    
//    GHAssertTrue(_rslt, @"Should be connected");
//}
//
//-(void)test_isSocketConnected_ifDisConnected
//{
//    //Precondition: testing equipment's network should be on;
//    BOOL _rslt=[testNetWorkHandler_ initConn];
//    
//    [NSThread sleepForTimeInterval:5.0];
//    
//    //initiative break connection
//    
//    [testNetWorkHandler_ disConnect];
//    
//    _rslt=[testNetWorkHandler_ isSocketConnected];
//    
//    GHAssertFalse(_rslt, @"Should be disConnected");
//}
//
//-(void)test_isSocketConnected_withHandlerUnInitialized
//{
//    testNetWorkHandler_=nil;
//    
//    BOOL _rslt=[testNetWorkHandler_ isSocketConnected];
//    
//    GHAssertFalse(_rslt,@"With nil handler and nil socket ,should be judge to unConnected");
//    
//    
//}
//-(void)test_isSocketConnected_withNilSocket
//{
//    BOOL _rslt=[testNetWorkHandler_ isSocketConnected];
//    
//    GHAssertFalse(_rslt,@"With nil socket ,should be judge to unConnected");
//}
//
//#pragma mark - test sendData
//
///*
// sendData test issue:
//    before send:
//    1、nil data passing in 
//    2、illegal pid passing in 
//    2、socket connected?
//    3、sendBuf_ nil? 
//    after send:
//    1、sendBuf_ reset?
//    error handle?
// */
//
//-(void)test_sendData_NilOrIllegalParameterPassing
//{
//    BOOL _rslt=[testNetWorkHandler_ sendData:nil Pid:8];
//    GHAssertFalse(_rslt, @"illegal Pid should not go through sendData Function");
//    
//    _rslt=[testNetWorkHandler_ sendData:nil Pid:0];
//    GHAssertFalse(_rslt, @"Nil data should not go through sendData Function");
//    
//    NSData *_testJustAllocData=[NSData alloc];
//    _rslt=[testNetWorkHandler_ sendData:_testJustAllocData Pid:0];
//    GHAssertFalse(_rslt, @"JustAllocData should not go through sendData Function");
//}
//
//-(void)test_sendData_SocketUnConnected
//{
//    //PreCondition :Socket is not connected
//    //struct test data
//    User *_testUser=[[[[[User builder]setAccount:@"testUserAccount"]setPassword:@"testUserPassword"]setAction:ActionCreate]build];
//    
//    NSData *_testUserData=[_testUser data];
//    
//    BOOL _rslt=[testNetWorkHandler_ sendData:_testUserData Pid:PIDUser];
//    
//    GHAssertFalse(_rslt, @"Should not Write if socket is not connected");
//}
//
//-(void)test_sendData_SendBufNil
//{
//    
//    BOOL _rslt=[testNetWorkHandler_ initConn];
//    
//    [NSThread sleepForTimeInterval:5.0];
//    
//    _rslt=[testNetWorkHandler_ isSocketConnected];
//    
//    GHAssertTrue(_rslt, @"Should be connected");
//    
//    
//    //struct test data
//    User *_testUser=[[[[[User builder]setAccount:@"testUserAccount"]setPassword:@"testUserPassword"]setAction:ActionCreate]build];
//    
//    NSData *_testUserData=[_testUser data];
//    
//    //set sendBufNil
//    
//    testNetWorkHandler_.sendBuf_=nil;
//    
//    _rslt=[testNetWorkHandler_ sendData:_testUserData Pid:PIDUser];
//    
//    GHAssertTrue(_rslt,@"Even sendBuf_ is nil ,should has send");
//
//    [NSThread sleepForTimeInterval:1.0];
//    
//    //NSUInteger _sendBufLen=[testNetWorkHandler_.sendBuf_ length];
//
//    //GHTestLog(@"sendBufLen:%ud",_sendBufLen);
//    //GHAssertLessThanOrEqual(, 0,@"sendBuf_ length should be 0 after sendData");
//}
//
///*
// readWithSpecificSize test issue:
//    before read:
//    1、size valid?
//    2、tag correct?
//    3、socket Connected?
// 
//    after read:
//    to handle_read stuff
//    
// */
//
//
//-(void)test_readWithSpecificSize_SizeValid
//{
//    //zero
//    BOOL _rslt=[testNetWorkHandler_ readWithSpecificSize:0 tag:READ_HEADER];
//    GHAssertFalse(_rslt, @"0 size should not go through");
//    
//    //negative
//    _rslt=[testNetWorkHandler_ readWithSpecificSize:-5 tag:READ_HEADER];
//    GHAssertFalse(_rslt, @"negative size should not go through");
//}
//
//-(void)test_readWithSpecificSize_TagCorrection
//{
//    BOOL _rslt=[testNetWorkHandler_ readWithSpecificSize:HEADER_SIZE tag:5454];
//    
//    GHAssertFalse(_rslt, @"InCorrect Tag should not go through");
//}
//
//-(void)test_readWithSpecificSize_SocketConnection
//{
//    //while socket is not connected
//    
//    BOOL _rslt=[testNetWorkHandler_ readWithSpecificSize:HEADER_SIZE tag:READ_HEADER];
//    
//    GHAssertFalse(_rslt, @"Socket UnConnected should not go through");
//    
//    //to connect
//    
//    _rslt=[testNetWorkHandler_ initConn];
//    
//    [NSThread sleepForTimeInterval:5.0];
//    
//    _rslt=[testNetWorkHandler_ isSocketConnected];
//    
//    GHAssertTrue(_rslt, @"Should be connected");
//    
//    
//    _rslt=[testNetWorkHandler_ readWithSpecificSize:HEADER_SIZE tag:READ_HEADER];
//    
//    GHAssertTrue(_rslt, @"With size,tag,socket Connected,should go through this function");
//    
//}
//
//
///*
// parseHeader test issue:
//    1,nil data?
//    2,illegal data?
//    3,shouldRecvBytes and recvPID should be set and correct
//    
// */
//
//-(void)test_parseHeader_DataCorrection
//{
//    BOOL _rslt=[testNetWorkHandler_ parseHeader:nil];
//    
//    GHAssertFalse(_rslt, @"Nil Data should not go through");
//    
//    NSString *_testStr=@"testString";
//    NSData *_testStrData=[_testStr dataUsingEncoding:NSASCIIStringEncoding];
//    
//    _rslt=[testNetWorkHandler_ parseHeader:_testStrData];
//    
//    GHAssertFalse(_rslt,@"Illegal data should not go through");
//    
//}
//
//-(void)test_parseHeader_CheckVariablesAfterParse
//{
//    
//    
//}
//
////any exception in any test case comes down to here
///*-(void)failWithException:(NSException *)exception
//{
//    if(exception)
//    {
//        GHTestLog(@"Exception:%@",[exception name],[exception reason]);
//    }
//}*/
//@end
